<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>身份证</title>
		<meta http-equiv="Access-Control-Allow-Origin" content="*">
		<meta http-equiv="content-security-policy">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<!--标准mui.css-->
		<link rel="stylesheet" href="../css/mui.min.css">
		<link rel="stylesheet" href="../css/common.css" />
		<style>* {
	margin: 0px;
	padding: 0px;
}

body {
	background-color: #FFFFFF;
}

.mui-content {
	background-color: #FFFFFF;
}

.header-container {
	margin-bottom: 20px;
	margin-top: 20px;
	width: 100%;
	text-align: -webkit-center;
}

.header-container .title {
	margin-left: 10px;
	margin-bottom: 10px;
	font-family: 'PingFangSC-Medium';
	font-size: 16px;
	color: #4A4A4A;
	text-align: left;
}

.header-div {
	margin-left: 10px;
	display: inline-block;
	width: 46%;
	height: 110px;
	border: 1px solid #F3F3F3;
	text-align: -webkit-center;
}

.camera-img {
	width: 40px;
	height: 40px;
	margin-top: 35px;
	background-image: url(../img/detail/camera_pressed.png);
	background-size: 100% 100%;
}

.camera-img input::-webkit-file-upload-button {
	width: 55px;
	height: 55px;
	border: none;
	position: absolute;
	outline: 0;
	opacity: 0;
}

.camera-img input {
	display: block;
	width: auto;
	border: 0;
	vertical-align: middle;
}

.line-div {
	width: 100%;
	height: 1px;
	background-color: #F3F3F3;
}

.tips-container {
	margin-top: 30px;
	margin-left: 25px;
}

.tips-container .title {
	font-family: 'PingFangSC-Medium';
	font-size: 16px;
	color: #4A4A4A;
	margin-bottom: 5px;
}

.tips-container .content {
	margin-left: 12px;
	font-family: 'PingFangSC-Regular';
	font-size: 12px;
	color: #4A4A4A;
	line-height: 25px;
}

.sample-div {
	display: inline-block;
	width: 46%;
	height: 110px;
}

.sample-img {
	width: 100%;
	height: 110px;
}

.select-card-container {
	width: 100%;
}

.select-check-box {
	height: 45px;
	margin: 10px 10px 0px 10px;
}

.select-check-box .title {
	font-family: 'PingFangSC-Medium';
	font-size: 14px;
	color: #4A4A4A;
	line-height: 45px;
}

.checkbox-div {
	position: relative;
	display: inline-block;
	float: right;
	width: 50px;
	height: 45px;
}

input[type="checkbox"] {
	-webkit-appearance: none;
	outline: none;
	border-width: 0px;
}

input.check {
	background: url(../img/detail/unselect_icon.png) no-repeat center left;
	background-size: 20px 20px;
	position: absolute;
	top: 50%;
	left: 10px;
	margin-top: -18px;
	width: 22px;
	height: 35px;
}

input.check:checked {
	background: url(../img/detail/selected_icon.png) no-repeat center left;
	background-size: 20px 20px;
}

.checked {
	background: url(../img/detail/selected_icon.png) no-repeat left center;
	background-size: 20px 20px;
	position: absolute;
	top: 50%;
	left: 15px;
	margin-top: -18px;
	width: 22px;
	height: 35px;
}

.card-status-div {
	position: relative;
}

.item-status {
	position: relative;
	width: 50px;
	height: 45px;
}

.mui-card {
	display: none;
	margin: 0px;
	box-shadow: 0px 0px 3px rgba(0, 0, 0, .3);
}

.mui-checkbox input[type=checkbox],
.mui-radio input[type=radio] {
	top: 9px;
}

.mui-checkbox input[type=checkbox]:before,
.mui-radio input[type=radio]:before {
	font-size: 18px;
}

.mui-input-row label {
	margin-left: 20px;
}

.transaction-container {
	display: none;
}</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">身份证</h1>
		</header>
		<div class="mui-content">
			<div class="select-card-container">
				<div class="select-check-box">
					<span class="title">没有身份证</span>
					<div class="checkbox-div">
						<input type="checkbox" class="check goods-check" id="noCardCheck">
					</div>
				</div>
				<div class="line-div"></div>
				<div class="mui-card">
					<form class="mui-input-group">
						<div class="mui-input-row mui-radio">
							<label>未满16周岁，尚未办理身份证</label>
							<input name="radio1" type="radio" id="nonage" >
						</div>
						<div class="mui-input-row mui-radio">
							<label>身份证丢失</label>
							<input name="radio1" type="radio" id="lose">
						</div>
					</form>
				</div>
			</div>
			<div class="header-container">
				<div class="title">身份证正面</div>
				<div class="sample-div">
					<img class="sample-img" src="../img/sample/idcard_zheng_img.png" />
				</div>
				<div id="frontImg" class="header-div">
					<div class="camera-img">
						<input type="file" name="fileFront" id="fileFront" accept="image/*" onchange="handleInputChange(this);" />
					</div>
				</div>
			</div>
			<div class="line-div"></div>
			<div class="header-container">
				<div class="title">身份证反面</div>
				<div class="sample-div">
					<img class="sample-img" src="../img/sample/idcard_fan_img.png" />
				</div>
				<div id="backImg" class="header-div">
					<div class="camera-img">
						<input type="file" name="fileBack" id="fileBack" accept="image/*" onchange="handleInputChange(this);" />
					</div>
				</div>
			</div>
			<div class="tips-container">
				<div class="title">资料要求</div>
				<div class="content">.清晰拍摄在有效期内的身份证反面</div>
			</div>
			<div class="transaction-container"><button type="button">确认</button></div>
		</div>
	</body>
	<script type="text/javascript" src="../js/thirdparty/jquery-3.3.1.js" ></script>
	<script type="text/javascript" src="../js/thirdparty/mui.min.js" ></script>
	<script type="text/javascript" src="../js/thirdparty/spin.min.js" ></script>
	<script type="text/javascript" src="../js/utils/boot.js" ></script>
	<script type="text/javascript">
		var customerId = base.getParameter("customerId");
		var applyCustomerId = base.getParameter("applyCustomerId");
		var cardType = 0;
		var cardId = "";
		$(function() {
			getIDCardInfoData();
		})
		
		function getIDCardInfoData() {
			var url = base.url.getIDCardInfo;
			var data = {
				applyCustomerId: applyCustomerId
			};
			base.postData(url, data, getIDCardInfoDataSuccess);
		}
		
		function getIDCardInfoDataSuccess(data) {
			if(data.success) {
				cardId = data.extendData.cardId;
				if(data.extendData.cardType == 1) {
					$("#frontImg").css("background-image", "url(" + data.extendData.IdCardFrontUrl + ")");
					$("#frontImg").css("background-size", "100% 100%");
					$("#backImg").css("background-image", "url(" + data.extendData.IdCardBackUrl + ")");
					$("#backImg").css("background-size", "100% 100%");
				} else {
					$("#noCardCheck").prop("checked");
					$('.mui-card').show();
					if(data.extendData.cardType == 2) {
						$("#nonage").prop("checked");
					} else {
						$("#lose").prop("checked");
					}
				}
			} else {
				mui.toast(data.message);
			}
		}
		$(".transaction-container").on("tap", "button", function() {
			var url = base.url.delIDCardInfo;
			var params = {
				cardId: cardId,
				type: 3
			};
			base.postData(url, params, function(data) {
				mui.toast(data.message);
				if(data.success) {
					setTimeout(function() {
						mui.back();
					}, 1000)
				}
			});
		})
		// 点击全选按钮
		$("#noCardCheck").click(function() {
			if($(this).prop("checked") == true) { //如果全选按钮被选中
				$('.transaction-container').show();
				$('.mui-card').show();
			} else {
				$('.transaction-container').hide();
				$('.mui-card').hide();
			}
		});
		
		function handleInputChange(file) {
			cardType = ($(file).attr("name") == "fileFront") ? 1 : 2;
			var base64, file = file.files[0];
			var maxSize = 1024 * 1024 * 8; //最大不超过8M
			var imgSize = file.size;
			if(maxSize < imgSize) {
				alert('上传图片不能超过8M');
				file.value = '';
				return
			}
			var reader = new FileReader();
			reader.onload = function(e) {
				base64 = e.target.result;
				canvasDataURL(base64);
			}
			reader.readAsDataURL(file);
		}
		//利用canvas压缩图片，根据画布大小和图像质量压缩
		function canvasDataURL(base64) {
			var img = new Image();
			img.src = base64;
			img.onload = function() {
				//默认按照比例压缩
				var scale = this.width / this.height;
				//规定压缩后的大小
				var canvasWidth = 400;
				var canvasHeight = canvasWidth / scale;
				//生成canvas
				var canvas = document.createElement('canvas');
				var ctx = canvas.getContext('2d');
				//创建节点属性
				canvas.width = canvasWidth;
				canvas.height = canvasHeight;
				var anw = document.createAttribute('width');
				anw.nodeValue = canvasWidth;
				var anh = document.createAttribute('height');
				anh.nodeValue = canvasHeight;
				canvas.setAttributeNode(anw);
				canvas.setAttributeNode(anh);
				ctx.drawImage(this, 0, 0, canvasWidth, canvasHeight);
				//图像质量,值越小，所绘制出的图像越模糊
				var quality = 0.92;
				var base64String = canvas.toDataURL('img/jpeg', quality);
				//通过base64获取二进制文件
				var blob = getBlobByBase64(base64String);
				//上传图片
				uploadImg(blob);
			}
		}
		//通过base64获取二进制文件
		function getBlobByBase64(base64String) {
			var arr = base64String.split(','),
				mine = arr[0].match(/:(.*?);/)[1],
				bstr = atob(arr[1]),
				n = bstr.length,
				u8arr = new Uint8Array(n);
			while(n--) {
				u8arr[n] = bstr.charCodeAt(n);
			}
			return new Blob([u8arr], {
				type: mine
			});
		}
		//上传图片
		function uploadImg(blob) {
			base.setWait("正在上传，请稍等...");
			var urlPath = cardType == 1 ? 'addIDCardFront' : 'addIDCardBack';
			var url = root_host + 'ry-web-admin/api/visaIdentityCard/' + urlPath; //需要上传的接口地址
			var fd = new FormData();
			//上传文件参数(看接口需要传什么)
			fd.append('file', blob, 'authonValidate.jpeg');
			fd.append('applyCustomerId', applyCustomerId);
			fd.append('customerId', customerId);
			fd.append('cardId', cardId);
			//XMLHttpRequest
			var xhr = new XMLHttpRequest();
			xhr.open('post', url);
			//请求头参数（看接口需要传什么）
			//		    xhr.setRequestHeader('token',sessionStorage.getItem('token'));
			//		    xhr.setRequestHeader('comId',sessionStorage.getItem('comId'));
			xhr.onreadystatechange = function() {
				console.log(xhr);
				if(xhr.readyState != 4) {
					return;
				}
				var data = JSON.parse(xhr.responseText);
				base.clearWait();
				if(data.success) {
					cardId = data.extendData.cardId;
					if(cardType == 1) {
						console.log(data.extendData.idCardFrontUrl);
						$("#frontImg").css("background-image", "url(" + data.extendData.idCardFrontUrl + ")");
						$("#frontImg").css("background-size", "100% 100%");
					} else {
						console.log(data.extendData.idCardBackUrl);
						$("#backImg").css("background-image", "url(" + data.extendData.idCardBackUrl + ")");
						$("#backImg").css("background-size", "100% 100%");
					}
				} else {
					mui.toast(data.message);
				}
			}
			xhr.send(fd);
		}
	</script>
</html>